package com.cw.base.jeeyc.biz.converter;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cw.base.jeeyc.dao.model.DeployDictItemDo;
import com.cw.base.jeeyc.deploy.dto.DeployDictItemDto;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.wrapper.BaseWrapper;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;

import cn.hutool.core.util.StrUtil;

/**
 * 数据字典项转换类
 *
 * @author chenw
 * @date 2019-08-26 21:21:57
 */
public class DeployDictItemConverter {

    /**
     * do转dto
     *
     * @param model
     * @return
     */
    public static DeployDictItemDto do2Dto(DeployDictItemDo model) {
        if (model == null) {
            return null;
        }
        return DeployDictItemDto.builder()
            .dictKey(model.getDictKey())
            .itemKey(model.getItemKey())
            .itemName(model.getItemName())
            .itemDesc(model.getItemDesc())
            .build();
    }

    /**
     * dto转do
     *
     * @param dto
     * @return
     */
    public static DeployDictItemDo dto2Do(DeployDictItemDto dto) {
        if (dto == null) {
            return null;
        }
        return DeployDictItemDo.builder()
            .dictKey(dto.getDictKey())
            .itemKey(dto.getItemKey())
            .itemName(dto.getItemName())
            .itemDesc(dto.getItemDesc())
            .build();
    }

    /**
     * doList转dtoList
     *
     * @param doList
     * @return
     */
    public static List<DeployDictItemDto> doList2DtoList(List<DeployDictItemDo> doList) {
        if (CollectionUtil.isEmpty(doList)) {
            return null;
        }
        return Lists.newArrayList(Iterators.transform(doList.iterator(), DeployDictItemConverter::do2Dto));
    }

    /**
     * dtoList转doList
     *
     * @param dtoList
     * @return
     */
    public static List<DeployDictItemDo> dtoList2DoList(List<DeployDictItemDto> dtoList) {
        if (CollectionUtil.isEmpty(dtoList)) {
            return null;
        }
        return Lists.newArrayList(Iterators.transform(dtoList.iterator(), DeployDictItemConverter::dto2Do));
    }

    public static LambdaQueryWrapper buildWrapper(DeployDictItemDto request) {
        LambdaQueryWrapper<DeployDictItemDo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        BaseWrapper<DeployDictItemDo> baseWrapper = new BaseWrapper<>();
        baseWrapper.buildBaseWrapper(lambdaQueryWrapper, request);
        if (StringUtils.isNotBlank(request.getDictKey())) {
            lambdaQueryWrapper.eq(DeployDictItemDo::getDictKey, request.getDictKey());
        }
        if (CollectionUtil.isNotEmpty(request.getDictKeys())) {
            lambdaQueryWrapper.in(DeployDictItemDo::getDictKey, request.getDictKeys());
        }
        if (StrUtil.isNotBlank(request.getItemKey())) {
            lambdaQueryWrapper.eq(DeployDictItemDo::getItemKey, request.getItemKey());
        }
        if (StrUtil.isNotBlank(request.getItemName())) {
            lambdaQueryWrapper.eq(DeployDictItemDo::getItemName, request.getItemName());
        }
        return lambdaQueryWrapper;
    }
}
